package com.example.Lezedata;

import android.content.Context;
import android.text.TextUtils;

import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.ui.activity.login.LoginVc;
import com.example.Lezedata.ui.tabBar.GBTabBarVc;
import com.example.Lezedata.util.GBActivityManager;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class GBAppInit {
    // app 启动需要设置的一些参数等
    public static void AppInit (){
        GBNetRequest.newInstance();
        GBActivityManager.sharedManager();
        initCrashReport();
    }

    static void initCrashReport(){
        Context context = GBApplication.getContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));

        boolean isDebug = GBApplication.isDebug();
        Beta.canShowUpgradeActs.add(GBTabBarVc.class);
        Beta.canShowUpgradeActs.add(LoginVc.class);
        Bugly.init(context,"21c6818268",isDebug,strategy);
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
}
